home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTDELCUR.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  106 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btdelcur.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <bool.h>
  8. #include <errno.h>
  9. /*#include <stddef.h>*/
  10. /*#include <string.h>*/
  11.  
  12. /* local headers */
  13. #include "btree_.h"
  14.  
  15. /*man---------------------------------------------------------------------------
  16. NAME
  17.      btdelcur - delete current btree key
  18.  
  19. SYNOPSIS
  20.      #include <btree.h>
  21.  
  22.      int btdelcur(btp)
  23.      btree_t *btp;
  24.  
  25. DESCRIPTION
  26.      The btdelcur function deletes the current key from btree btp.
  27.      The cursor is positioned to null.
  28.  
  29.      btdelcur will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       btp is not a valid btree pointer.
  32.      [BTELOCK]      btree btp is not write locked.
  33.      [BTENKEY]      The cursor is null.
  34.      [BTENOPEN]     btree btp is not open.
  35.  
  36. SEE ALSO
  37.      btdelete, btinsert, btsearch.
  38.  
  39. DIAGNOSTICS
  40.      Upon successful completion, a value of 0 is returned.  Otherwise,
  41.      a value of -1 is returned, and errno set to indicate the error.
  42.  
  43. ------------------------------------------------------------------------------*/
  44. int btdelcur(btp)
  45. btree_t *btp;
  46. {
  47.     int        found    = 0;        /* key found flag */
  48.     void *        key    = NULL;        /* key */
  49.  
  50.     /* validate arguments */
  51.     if (!bt_valid(btp)) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     /* check if not open */
  57.     if (!(btp->flags & BTOPEN)) {
  58.         errno = BTENOPEN;
  59.         return -1;
  60.     }
  61.  
  62.     /* check lock */
  63.     if (!(btp->flags & BTWRLCK)) {
  64.         errno = BTELOCK;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if cursor is null */
  69.     if (btcursor(btp) == NULL) {
  70.         errno = BTENKEY;
  71.         return -1;
  72.     }
  73.  
  74.     /* generate search path if necessary */
  75.     if (btp->cbtnp->n < bt_ndmin(btp) + 1) {
  76.         key = calloc((size_t)1, btp->bthdr.keysize);
  77.         if (key == NULL) {
  78.             BTEPRINT;
  79.             return -1;
  80.         }
  81.         memcpy(key, bt_kykeyp(btp, btp->cbtnp, btp->cbtpos.key), btp->bthdr.keysize);
  82.         found = btsearch(btp, key);
  83.         if (found == -1) {
  84.             BTEPRINT;
  85.             free(key);
  86.             return -1;
  87.         }
  88.         free(key);
  89.         key = NULL;
  90.         if (found == 0) {
  91.             BTEPRINT;
  92.             errno = BTEPANIC;
  93.             return -1;
  94.         }
  95.     }
  96.  
  97.     /* delete the current key */
  98.     if (bt_delete(btp) == -1) {
  99.         BTEPRINT;
  100.         return -1;
  101.     }
  102.  
  103.     errno = 0;
  104.     return 0;
  105. }
  106.